home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
vc
/
pro3
/
qsort-ex.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-10
|
2KB
|
59 lines
===========================================================================
BBS: The Abacus * HST/DS * Potterville, MI
Date: 06-07-93 (18:20) Number: 177
From: ALLAN NELSON Refer#: NONE
To: GEOFFREY LIU Recvd: NO
Subj: Re: Problems with qsort Conf: (36) C Language
---------------------------------------------------------------------------
-=> Quoting Geoffrey Liu to All <=-
GL> I hope someone here can point out what I'm doing wrong. I'm
GL> trying to sort a structure with one of the elements as the "item"
GL> being sorted. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_function( const void *a, const void *b);
struct test {
char zip;
char list2[4];
char nothing;
} list[5] = { 0,"cat",0,0, "car",0, 0,"cab",0, 0,"cap",0, 0,"can",0 };
typedef struct test *tptr;
int main(void) {
int x;
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x++)
printf("%s\n", list[x].list2);
return 0;
}
int sort_function( const void *ab, const void *bc){
tptr t_a, t_b;
printf ("ab: %p bc: %p ",ab,bc);
t_a = ab;
t_b = bc;
return( strcmp(t_a->list2, t_b->list2));
}
The problem is that you have passed pointers of type void and what
the compiler wants is pointers to structures. This changed code
works with warnings under BC++v3.1. You could clean up the problem
by passing pointers to structures to sort_function.
... "Scotty, beam me up another Blue Wave message."
___ Blue Wave/QWK v2.12
--- Maximus/2 2.01wb
* Origin: The Programmers Retreat (1:382/17)
SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20